package com.salestax.core.visitor;

import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;

import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.salestax.core.Product;
import com.salestax.core.ShoppingList;

public class ReceiptPrinter implements ShoppingListVisitor {

	private Writer writer;
	public ReceiptPrinter(Writer writer) {
		super();
		this.writer = writer;
	}
	@Override
	public void visit(ShoppingList shoppingList) {
		try {
			for(Product product:shoppingList.getProducts()){
				writer.write(String.valueOf(product.getQuantity()));
				writer.write(" ");
				writer.write(product.getDescription());
				writer.write(":");
				writer.write(product.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
				writer.write("\n");
			}
			writer.write("Sales Taxes :"+shoppingList.totalTax().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
			writer.write("\n");
			writer.write("Total :"+shoppingList.totalCost().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
			
			//Lists.
		
		} catch (IOException e) {
			System.err.print("Unexpected IO Error");
		}
	}
	
	

}
